Ext.ns('Tm.bill');
Tm.bill.BillFeePanel = Ext.extend(Ext.Panel, {
	id : 'billFeePanel',
	frame : true,
    autoHeight:true,
    autoWidth:true,
	layout : 'fit',
	billId : null,
	initComponent : function() {
		var taskForm = new Tm.common.FormPanel({
			title:'基本信息',
			collapsible :true,
					ref : 'taskForm',
					layout : 'table',
					layoutConfig : {
						columns : 3
					},
					defaults : {
						xtype : 'textfield',
						margins : '0 5 0 0'
					},
					items : [ {
								plugins : [Ext.ux.FieldLabeler],
								name : 'taskName',
								colspan:2,
								width:450,
								fieldLabel : '案件名称',
								readOnly : true
		
							},{
								plugins : [Ext.ux.FieldLabeler],
								name : 'code',
								fieldLabel : '案件编号',
								readOnly : true

							}, {
								plugins : [Ext.ux.FieldLabeler],
								name : 'ownerName',
								fieldLabel : '负责律师',
								readOnly : true

							}, {
								plugins : [Ext.ux.FieldLabeler],
								name : 'createName',
								fieldLabel : '立案人',
								readOnly : true

							}, {
								plugins : [Ext.ux.FieldLabeler],
								name : 'createTime',
								fieldLabel : '立案日期',
								renderer : Ext.util.Format.customDateRenderer('Y-m-d'), 
								readOnly : true

							}, {
								plugins : [Ext.ux.FieldLabeler],
								name : 'compName',
								fieldLabel : '客户名称',
								readOnly : true

							}, {
								plugins : [Ext.ux.FieldLabeler],
								name : 'contactorName',
								fieldLabel : '联络人',
								readOnly : true

							}, {
								plugins : [Ext.ux.FieldLabeler],
								name : 'contactorTel',
								fieldLabel : '联系电话',
								readOnly : true

							}, {
								plugins : [Ext.ux.FieldLabeler],
								name : 'contactorMobile',
								fieldLabel : '移动电话',
								readOnly : true
								
							}, {
								plugins : [Ext.ux.FieldLabeler],
								name : 'contactorFax',
								fieldLabel : '传真',
								readOnly : true

							}, {
								plugins : [Ext.ux.FieldLabeler],
								name : 'contactorEmail',
								fieldLabel : '电子邮件',
								readOnly : true

							}

					]
				});
		var billForm = new Tm.common.FormPanel({
			title:'账单信息',
			ref : 'billForm',
			collapsible :true,
			layout : 'table',
			layoutConfig : {
				columns : 3
			},
			defaults : {
				xtype : 'textfield',
				margins : '0 5 0 0'
			},
			items : [{
						plugins : [Ext.ux.FieldLabeler],
						name : 'code',
						fieldLabel : '账单编号',
						readOnly : true

					}, {
						plugins : [Ext.ux.FieldLabeler],
						name : 'stauts',
						fieldLabel : '状态',
						width:225,
						xtype : 'combo',
						mode : 'local',
						editable : false,
						typeAhead : true,
						forceSelection : true,
						hiddenName : 'stauts',
						mode : 'local',
						triggerAction : 'all',
						emptyText : '请选择...',
						selectOnFocus : true,
						store : Tm.data.billStatus,					
						readOnly : true

					}, {
						plugins : [Ext.ux.FieldLabeler],
						name : 'currency',
						fieldLabel : '币种',
						width:225,
						xtype : 'combo',
						mode : 'local',
						editable : false,
						typeAhead : true,
						forceSelection : true,
						hiddenName : 'currency',
						mode : 'local',
						triggerAction : 'all',
						emptyText : '请选择...',
						selectOnFocus : true,
						store : Tm.data.currency,					
						readOnly : true

					}, {
						plugins : [Ext.ux.FieldLabeler],
						name : 'total',
						fieldLabel : '应付金额',
						readOnly : true,
						xtype : 'numberfield',
						decimalPrecision:2,
						allowDecimals:true,
		                allowNegative:false  
					}, {
						plugins : [Ext.ux.FieldLabeler],
						name : 'paid',
						fieldLabel : '已付金额',
						readOnly : true,
 						xtype : 'numberfield',
						decimalPrecision:2,
						allowDecimals:true, 
		                allowNegative:false 

					}, {
						plugins : [Ext.ux.FieldLabeler],
						name : 'unPaid',
						fieldLabel : '未付金额',
						readOnly : true,
 						xtype : 'numberfield',
						decimalPrecision:2,
						allowDecimals:true, 
		                allowNegative:false

					}, {
						plugins : [Ext.ux.FieldLabeler],
						name : 'createName',
						fieldLabel : '创建人',
						readOnly : true

					}, {
						plugins : [Ext.ux.FieldLabeler],
						name : 'createTime',
						fieldLabel : '创建时间',
						//colspan:2,
						readOnly : true

					}, {
						plugins : [Ext.ux.FieldLabeler],
						name : 'deadline',
						fieldLabel : '付款期限',
						readOnly : true

					}, {
						plugins : [Ext.ux.FieldLabeler],
						name : 'chargeoffTime',
						fieldLabel : '出账时间',
						readOnly : true

					}, {
						plugins : [Ext.ux.FieldLabeler],
						name : 'paidTime',
						fieldLabel : '收讫时间',
						colspan:2,
						readOnly : true

					}, {
						plugins : [Ext.ux.FieldLabeler],
						name : 'description',
						fieldLabel : '描述',
						width:450,
						colspan:3,
						readOnly : true

					}

			]
		});
		
		var detailsm=new Ext.grid.CheckboxSelectionModel();
		var billDetailColumnModel = new Ext.grid.ColumnModel([detailsm,{
			header : '金额',
			width : 100,
			dataIndex : 'amount'
		}, {
			header : '创建人',
			width : 50,
			dataIndex : 'createName'
		}, {
			header : '创建时间',
			width : 75,
			renderer : Ext.util.Format.customDateRenderer('Y-m-d'), 
			dataIndex : 'createTime'
		}, {
			header : '更新人',
			width : 50,
			dataIndex : 'lastUpdateName'
		}, {
			header : '更新时间',
			width : 75,
			renderer : Ext.util.Format.customDateRenderer('Y-m-d'), 
			dataIndex : 'lastUpdateTime'
		}, {
			header : '描述',
			width : 250,
			dataIndex : 'description'
		}]
		);
		billDetailColumnModel.defaultSortable = true;
		var fields = [{
					name : 'billDetailId',
					type : 'string',
					mapping : 'billDetailId'
				}, {
					name : 'amount',
					type : 'float',
					mapping : 'amount'
				}, {
					name : 'createName',
					type : 'string',
					mapping : 'createName'
				}, {
					name : 'createTime',
					type : 'string',
					mapping : 'createTime'

				}, {
					name : 'lastUpdateName',
					type : 'string',
					mapping : 'lastUpdateName'
				}, {
					name : 'lastUpdateTime',
					type : 'string',
					mapping : 'lastUpdateTime'
				}, {
					name : 'description',
					type : 'string',
					mapping : 'description'
				}];
		
		
		var billDetailStore = new Ext.data.Store({
					proxy : new Ext.data.HttpProxy({
								url : 'bill/queryBillDetail.do'
							}),
					reader : new Ext.data.JsonReader({
								totalProperty : 'totalProperty',
								root : 'root',
								id : 'id',
								fields : fields
							}),
					baseParams : {
						billId:this.billId,
						start : 0,
						limit : 5
					},
//					sortInfo : {
//						field : 'billDetailId',
//						direction : 'ASC'
//					},
					autoLoad : true
//					,
//					listeners: {
//				        beforeload: function() {
//				        		//Ext.apply(this.baseParams, form.getForm().getFieldValues(false));
//				        },
//						datachanged :function(store) {
//							var billFeePanel = Ext.getCmp("billFeePanel");
//
//							Ext.Ajax.request({
//								url : 'bill/getTotalAmountByBillId.do',
//								params : {
//									billId : billFeePanel.billId
//								},
//								success : function(response, action) {
//									var total=response.responseText;
//									billForm.getForm().findField("total").setValue(total);
//								},
//								failure : function(response, action) {
//									App.setAlert('error', '载入失败');
//								}});
//							
//						}
//				    }
				}


		);
		var billDetailToolBar = new Ext.Toolbar({
			hideBorders : true,
			buttons : [ {
						text : '新增明细',
						iconCls : 'act_addIcon',
						handler : function() {
							var billFeePanel=Ext.getCmp('billFeePanel');
							var billDetailAddPanel = new Tm.bill.BillDetailAddPanel({	
									billId : billFeePanel.billId,
									billstatus :billFeePanel.billForm.getForm().findField("stauts").getValue()
									});
							var billDetailAddWindow = new Tm.common.CommonWindow({
										id : 'billDetailAddWindow',
										title : '新增明细',
										items : billDetailAddPanel
									});
							 billDetailAddWindow.on('close', function(panel) {
									billDetailStore.load();
									billFeePanel.loadData(billFeePanel.billId);
							 });
							billDetailAddWindow.show();			
							
							
						}
					}, {
						xtype : 'tbseparator'
					}, {
						text : '删除明细',
						iconCls : 'act_deleteIcon',

						handler : function() {
							var items = billDetailGrid.getSelectionModel().selections.items;
							if (items.length == 0) {
								return;
							} 
							var ids=[];
							for(var i=0;i<items.length;i++){
								if(items[i].data.billDetailId){
									ids[i]=parseInt(items[i].data.billDetailId);
								}
							}
							Ext.MessageBox.confirm('注意', '您确定要进行该操作？',
									function(btn) {

										if (btn == 'yes') {

											Ext.Ajax.request({
												url : 'bill/deleteBillDetails.do',
												method : 'POST',
												params : {
													ids : ids
												},
												success : function(action,
														response) {
													billDetailStore.load();
													var billFeePanel=Ext.getCmp('billFeePanel');
													billFeePanel.loadData(billFeePanel.billId);
												},
												failure : function(action,
														response) {
													App.setAlert('error', '操作失败');

												}
											});
										}

									});

						}
					}]
		});
		var billDetailGrid = new Ext.grid.GridPanel({
					ref : 'billDetailGrid',
					height:210,
					title:'账单明细',
					collapsible :true,
					sm:detailsm,
					store : billDetailStore,
					cm : billDetailColumnModel,
					border : false,
					stripeRows : true,
					//autoHeight : true,
					viewConfig : {
						forceFit : true
					},
					tbar : billDetailToolBar,

					bbar : new Ext.PagingToolbar({
								plugins : new Ext.ux.ProgressBarPager(),
								pageSize : 5,
								store : billDetailStore,
								displayInfo : true,
								emptyMsg : '没有记录'
							}),

				listeners: {
				        celldblclick: function(grid ,rowIndex,columnIndex,event) {
				        	 var store=grid.getStore();
								 var data=store.getAt(rowIndex).data;
									// detail
									var billDetailAddPanel = new Tm.bill.BillDetailAddPanel(
											{	
												billDetailId : data.billDetailId,
												billstatus :Ext.getCmp('billFeePanel').billForm.getForm().findField("stauts").getValue()
											});
									var billDetailAddWindow = new Tm.common.CommonWindow({
												id : 'billDetailAddWindow',
												title : '更新账单明细',
												items : billDetailAddPanel
											});
									 billDetailAddWindow.on('close', function(panel) {
											billDetailStore.load();
									 });
									billDetailAddWindow.show();

								
								

								
				        }
				    }
				});
		
		
		
		
		
		
		
		
		
		var feesm=new Ext.grid.CheckboxSelectionModel()
		var feeColumnModel = new Ext.grid.ColumnModel([feesm, {
			header : '金额',
			width : 100,
			dataIndex : 'amount'
		}, {
			header : '凭证号',
			width : 100,
			dataIndex : 'code'
		}, {
			header : '创建人',
			width : 50,
			dataIndex : 'createName'
		}, {
			header : '创建时间',
			width : 75,
			renderer : Ext.util.Format.customDateRenderer('Y-m-d'), 
			dataIndex : 'createTime'
		}, {
			header : '更新人',
			width : 50,
			dataIndex : 'lastUpdateName'
		}, {
			header : '更新时间',
			width : 75,
			renderer : Ext.util.Format.customDateRenderer('Y-m-d'), 
			dataIndex : 'lastUpdateTime'
		}, {
			header : '描述',
			width : 250,
			dataIndex : 'description'
		}]
		);
		feeColumnModel.defaultSortable = true;
		var fields = [{
					name : 'id',
					type : 'int',
					mapping : 'id'
				}, {
					name : 'amount',
					type : 'float',
					mapping : 'amount'
				}, {
					name : 'code',
					type : 'string',
					mapping : 'code'
				}, {
					name : 'createName',
					type : 'string',
					mapping : 'createName'
				}, {
					name : 'createTime',
					type : 'string',
					mapping : 'createTime'

				}, {
					name : 'lastUpdateName',
					type : 'string',
					mapping : 'lastUpdateName'
				}, {
					name : 'lastUpdateTime',
					type : 'string',
					mapping : 'lastUpdateTime'
				}, {
					name : 'description',
					type : 'string',
					mapping : 'description'
				}];
		
		
		var feeStore = new Ext.data.Store({
					proxy : new Ext.data.HttpProxy({
								url : 'bill/queryFee.do'
							}),
					reader : new Ext.data.JsonReader({
								totalProperty : 'totalProperty',
								root : 'root',
								id : 'id',
								fields : fields
							}),
					baseParams : {
						billId:this.billId,
						start : 0,
						limit : 5
					},
//					sortInfo : {
//						field : 'id',
//						direction : 'ASC'
//					},
					autoLoad : true
//					,
//					listeners: {
//				        beforeload: function() {
//				        		//Ext.apply(this.baseParams, form.getForm().getFieldValues(false));
//				        },
//						datachanged :function(store) {
//							var billFeePanel = Ext.getCmp("billFeePanel");
//
//							Ext.Ajax.request({
//								url : 'bill/getPaidAmountByBillId.do',
//								params : {
//									billId : billFeePanel.billId
//								},
//								success : function(response, action) {
//									var total=response.responseText;
//									billForm.getForm().findField("paid").setValue(total);
//								},
//								failure : function(response, action) {
//									App.setAlert('error', '载入失败');
//								}});
//							
//						}
//				    }
				}


		);
		var feeToolBar = new Ext.Toolbar({
			hideBorders : true,
			buttons : [{
						text : '新增回款',
						iconCls : 'act_addIcon',
						handler : function() {
							var billFeePanel=Ext.getCmp('billFeePanel');
							var feeAddPanel = new Tm.bill.FeeAddPanel({	
									billId : billFeePanel.billId,
									billstatus :billFeePanel.billForm.getForm().findField("stauts").getValue()

									});
							var feeAddWindow = new Tm.common.CommonWindow({
										id : 'feeAddWindow',
										title : '新增回款',
										items : feeAddPanel
									});
							 feeAddWindow.on('close', function(panel) {
									feeStore.load();
									billFeePanel.loadData(billFeePanel.billId);
							 });
							feeAddWindow.show();			
							
							
						}
					}, {
						xtype : 'tbseparator'
					}, {
						text : '删除回款',
						iconCls : 'act_deleteIcon',

						handler : function() {
							var ids = feeGrid.getSelectionModel().selections.keys;
							if (ids.length == 0) {
								return;
							} 
							//var items = grid.getSelectionModel().selections.items;


							Ext.MessageBox.confirm('注意', '您确定要进行该操作？',
									function(btn) {

										if (btn == 'yes') {

											Ext.Ajax.request({
												url : 'bill/deleteFees.do',
												method : 'POST',
												params : {
													ids : ids
												},
												success : function(action,
														response) {
													feeStore.load();
													var billFeePanel=Ext.getCmp('billFeePanel');
													billFeePanel.loadData(billFeePanel.billId);
												},
												failure : function(action,
														response) {
													App.setAlert('error', '操作失败');

												}
											});
										}

									});

						}
					}]
		});
		var feeGrid = new Ext.grid.GridPanel({
					ref:'feeGrid',
					height:210,
					title:'回款列表',
					collapsible :true,
					sm:feesm,
					store : feeStore,
					cm : feeColumnModel,
					border : false,
					stripeRows : true,
					//autoHeight : true,
					viewConfig : {
						forceFit : true
					},
					tbar : feeToolBar,

					bbar : new Ext.PagingToolbar({
								plugins : new Ext.ux.ProgressBarPager(),
								pageSize : 5,
								store : feeStore,
								displayInfo : true,
								emptyMsg : '没有记录'
							}),

				listeners: {
				        celldblclick: function(grid ,rowIndex,columnIndex,event) {
				        	var billFeePanel=this;
				        	 var store=grid.getStore();
								 var data=store.getAt(rowIndex).data;
									var feeAddPanel = new Tm.bill.FeeAddPanel(
											{	
												feeId : data.id,
												billstatus :Ext.getCmp('billFeePanel').billForm.getForm().findField("stauts").getValue()

											});
									var feeAddWindow = new Tm.common.CommonWindow({
												id : 'feeAddWindow',
												title : '更新回款',
												items : feeAddPanel
											});
									 feeAddWindow.on('close', function(panel) {
											feeStore.load();
											billFeePanel.loadData(billFeePanel.billId);
									 });
									feeAddWindow.show();

								
								

								
				        },
						scope:Ext.getCmp('billFeePanel')
				    }
				});
		
		
		var toolBar = new Ext.Toolbar({
			hideBorders : true,
			buttons : [{
						id:'outButton',
						text : '出账',
						iconCls : 'act_outBill',
						handler : function() {
							var billFeePanel = Ext.getCmp('billFeePanel');
							billFeePanel.changeBillStatus('OUT');
						}
					}, {
						xtype : 'tbseparator'
					}, {
						id:'paidButton',
						text : '收讫',
						iconCls : 'act_paidBill',
						handler : function() {
							var billFeePanel = Ext.getCmp('billFeePanel');
							billFeePanel.changeBillStatus('PAID');
						}
					}, {
						xtype : 'tbseparator'
					}, {
						id:'badButton',
						text : '坏账',
						iconCls : 'act_badBill',
						handler : function() {
							var billFeePanel = Ext.getCmp('billFeePanel');
							billFeePanel.changeBillStatus('BAD');
						}
					}]
		});
		
		
		
		Ext.apply(this, {
					tbar : toolBar,
					items : [taskForm, billForm,billDetailGrid,feeGrid]
				});
		Tm.bill.BillFeePanel.superclass.initComponent.call(this);
	},
	loadData : function(billId) {
		if (billId) {
			Ext.Ajax.request({
						url : 'bill/getBillInfo.do',
						params : {
							billId : billId
						},
						success : function(response, action) {
							var billInfoVo = Ext.decode(response.responseText);
							var billFeePanel = Ext.getCmp("billFeePanel");
							billFeePanel.init(billInfoVo);

						},
						failure : function(response, action) {
							App.setAlert('error', '载入失败');
						}
						
					});
		}
	},
	changeBillStatus : function(status) {
		Ext.Ajax.request({
			url : 'bill/changeBillStatus.do',
			params : {
				billId : this.billId,
				status :status
			},
			success : function(response, action) {
				var bill  = Ext.decode(response.responseText);
				var billFeePanel = Ext.getCmp("billFeePanel");
				billFeePanel.loadData(bill.id);

			},
			failure : function(response, action) {
				App.setAlert('error', '载入失败');
			}
		});
	},
	init : function(billInfoVo) {
		var compInfoVo = billInfoVo.compInfoVo;
		var taskInfoVo = billInfoVo.taskInfoVo;
		var billFeePanel = Ext.getCmp("billFeePanel");
		var taskForm = billFeePanel.taskForm;
		var billForm = billFeePanel.billForm;
		var billDetailGrid = billFeePanel.billDetailGrid;
		var feeGrid = billFeePanel.feeGrid;

		taskForm.getForm().findField("code").setValue(taskInfoVo.code);
		taskForm.getForm().findField("taskName").setValue(taskInfoVo.taskName);
		taskForm.getForm().findField("ownerName").setValue(taskInfoVo.ownerName);
		taskForm.getForm().findField("createTime").setValue(Ext.util.Format.customFormatDateValue(taskInfoVo.createTime,'Y-m-d'));
		taskForm.getForm().findField("createName").setValue(taskInfoVo.createName);
		taskForm.getForm().findField("compName").setValue(compInfoVo.compName);
		taskForm.getForm().findField("contactorName").setValue(compInfoVo.contactorName);
		taskForm.getForm().findField("contactorTel").setValue(compInfoVo.contactorTel);
		taskForm.getForm().findField("contactorMobile").setValue(compInfoVo.contactorMobile);
		taskForm.getForm().findField("contactorFax").setValue(compInfoVo.contactorFax);
		taskForm.getForm().findField("contactorEmail").setValue(compInfoVo.contactorEmail);

		billForm.getForm().findField("code").setValue(billInfoVo.code);
		billForm.getForm().findField("stauts").setValue(billInfoVo.status);
		billForm.getForm().findField("currency").setValue(billInfoVo.currency);
		billForm.getForm().findField("total").setValue(billInfoVo.total);
		billForm.getForm().findField("paid").setValue(billInfoVo.paid);
		billForm.getForm().findField("unPaid").setValue(billInfoVo.unPaid);
		var unPaidFieldEl=Ext.get(billForm.getForm().findField("unPaid").id);
		if(billInfoVo.unPaid>0){
			unPaidFieldEl.applyStyles('color:red');
		}else{
			unPaidFieldEl.applyStyles('color:blue');
		}
 		
		billForm.getForm().findField("deadline").setValue(Ext.util.Format.customFormatDateValue(billInfoVo.deadline,'Y-m-d'));
		billForm.getForm().findField("chargeoffTime").setValue(Ext.util.Format.customFormatDateValue(billInfoVo.chargeoffTime,'Y-m-d'));
		billForm.getForm().findField("paidTime").setValue(Ext.util.Format.customFormatDateValue(billInfoVo.paidTime,'Y-m-d'));
		billForm.getForm().findField("createName").setValue(billInfoVo.createName);
		billForm.getForm().findField("createTime").setValue(Ext.util.Format.customFormatDateValue(billInfoVo.createTime,'Y-m-d'));
		billForm.getForm().findField("description").setValue(billInfoVo.description);
		
		var toolBar=billFeePanel.topToolbar;
		var detailToolbarItems=billDetailGrid.topToolbar.items.items;
		var feeToolbarItems=feeGrid.topToolbar.items.items;

		if(billInfoVo.status=='NEW'){
			for(var i=0;i<feeToolbarItems.length;i++){
				feeToolbarItems[i].setDisabled(true);
			}
			for(var i=0;i<detailToolbarItems.length;i++){
				detailToolbarItems[i].setDisabled(false);
			}
			toolBar.findById('outButton').setDisabled(false);
			toolBar.findById('paidButton').setDisabled(true);
			toolBar.findById('badButton').setDisabled(true);
		}else if(billInfoVo.status=='OUT'){
			for(var i=0;i<feeToolbarItems.length;i++){
				feeToolbarItems[i].setDisabled(false);
			}
			for(var i=0;i<detailToolbarItems.length;i++){
				detailToolbarItems[i].setDisabled(true);
			}
			toolBar.findById('outButton').setDisabled(true);
			toolBar.findById('paidButton').setDisabled(false);
			toolBar.findById('badButton').setDisabled(false);

		}else if(billInfoVo.status=='PAID'||billInfoVo.status=='BAD'){
			for(var i=0;i<feeToolbarItems.length;i++){
				feeToolbarItems[i].setDisabled(true);
			}
			for(var i=0;i<detailToolbarItems.length;i++){
				detailToolbarItems[i].setDisabled(true);
			}
			toolBar.findById('outButton').setDisabled(true);
			toolBar.findById('paidButton').setDisabled(true);
			toolBar.findById('badButton').setDisabled(true);
		}

	},
 	listeners : {
		afterrender : function() {
			this.loadData(this.billId);
		}
	}
});
